/**
 * 交易限制记录
 */
angular.module('inspinia',['infinity.angular-chosen','uiSwitch']).controller('transLimitWhiteQueryCtrl',function($scope,$http,i18nService,SweetAlert,$document,$timeout){
    i18nService.setCurrentLang('zh-cn');  //设置语言为中文
    $scope.paginationOptions=angular.copy($scope.paginationOptions);
    $scope.quotaStatus=0;
    $scope.transStatus=0;
    $scope.statusSelect = [{text:"全部",value:""},{text:"开启",value:"1"},{text:"关闭",value:"0"}];
    $scope.limitActiones= [{text:"全部",value:""},{text:"限制交易",value:"TRANS"},{text:"限制额度",value:"QUOTA"}];
    //清空
    $scope.clear=function(){
        $scope.info={merchantNo:"",mobilephone:"",status:"",limitAction:"",createPerson:"",
            createTimeBegin:moment(new Date().getTime()-6*24*60*60*1000).format('YYYY-MM-DD')+' 00:00:00',
            createTimeEnd:moment(new Date().getTime()).format('YYYY-MM-DD')+' 23:59:59'};
    };
    $scope.clear();

    $scope.userGrid={                           //配置表格
        data: 'result',
        paginationPageSize:10,                  //分页数量
        paginationPageSizes: [10,20,50,100],	//切换每页记录数
        useExternalPagination: true,		    //开启拓展名
        enableHorizontalScrollbar: true,        //横向滚动条
        enableVerticalScrollbar : true,  		//纵向滚动条
        columnDefs:[                           //表格数据
            { field: 'id',displayName:'ID',width:80 },
            { field: 'merchantNo',displayName:'商户编号',width:180 },
            { field: 'mobilephone',displayName: '商户手机号',width: 180},
            { field: 'status',displayName:'状态',width:180,
                cellTemplate:
                    '<span ng-show="grid.appScope.hasPermit(\'transLimit.changeTransLimitWhiteStatus\')"><switch class="switch switch-s" ng-model="row.entity.status" ng-true-value="1" ng-false-value="0" ng-change="grid.appScope.changeStatus(row.entity)" /></span>'+
                    '<span ng-show="!grid.appScope.hasPermit(\'transLimit.changeTransLimitWhiteStatus\')"> <span ng-show="row.entity.status==1" class="lh30" >开启</span><span ng-show="row.entity.status==0" class="lh30" >关闭</span></span>'
            },
            { field: 'limitAction',displayName:'白名单类型',width:180, cellFilter: "formatDropping:" + angular.toJson($scope.limitActiones)},

            { field: 'remark',displayName:'备注',width:180 },
            { field: 'createPerson',displayName:'创建人',width:180 },
            { field: 'createTime',displayName:'创建时间',cellFilter: 'date:"yyyy-MM-dd HH:mm:ss"',width:180},
            { field: 'action',displayName: '操作',width:180,pinnedRight:true,sortable: false,editable:true,cellTemplate:
                    '<a class="lh30" ng-show="grid.appScope.hasPermit(\'transLimit.updateTransLimitWhite\')" ui-sref="risk.updateTransLimitWhite({id:row.entity.id})"> 修改 </a> ' +
                    '<a class="lh30" ng-show="grid.appScope.hasPermit(\'transLimit.deleteTransLimitWhite\')" ng-click="grid.appScope.deleteTransLimitWhite(row.entity.id)"> 删除 </a> '
                    } ],
        onRegisterApi: function(gridApi) {
            $scope.gridApi = gridApi;
            gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                $scope.paginationOptions.pageNo = newPage;
                $scope.paginationOptions.pageSize = pageSize;
                $scope.query();
            });
        }
    };

    $scope.query=function(){
        if ($scope.loadImg) {
            return;
        }
        $scope.loadImg = true;
        $http.post("transLimit/selectTransLimitWhiteList","info="+angular.toJson($scope.info)+"&pageNo="+
            $scope.paginationOptions.pageNo+"&pageSize="+$scope.paginationOptions.pageSize,
            {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
            .success(function(data){
                if(data.status){
                    $scope.result=data.page.result;
                    $scope.userGrid.totalItems = data.page.totalCount;
                }else{
                    $scope.notice(data.msg);
                }
                $scope.loadImg = false;
            })
            .error(function(data){
                $scope.notice(data.msg);
                $scope.loadImg = false;
            });
    };
    $scope.query();

    //导出信息
    $scope.exportInfo=function(){
        SweetAlert.swal({
                title: "确认导出？",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "提交",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true
            },
            function (isConfirm) {
                if (isConfirm) {
                    $scope.exportInfoClick("transLimit/importTransLimitWhiteList",{"info":angular.toJson($scope.info)});
                }
            });
    };

    //状态当个改变
    $scope.changeStatus=function(entry){
        var title="";
        var sta=0;
        if(entry.status==1||entry.status==true){
            title="确认开启限制?";
            sta=1;
        }else{
            title="确认解除限制?";
            sta=0;
        }
        SweetAlert.swal({
                title: title,
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "提交",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                    $http.post("transLimit/updateTransLimitWhiteStatus","id="+entry.id+"&status="+sta,
                        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
                        .success(function(data){
                            if(data.status){
                                $scope.notice(data.msg);
                                $scope.query();
                            }else{
                                $scope.notice(data.msg);
                                entry.status = !entry.status;
                            }
                        })
                        .error(function(data){
                            $scope.notice(data.msg);
                            entry.status = !entry.status;
                        });
                }else{
                    entry.status = !entry.status;
                }
            });
    };

    //删除
    $scope.deleteTransLimitWhite=function(id){
        SweetAlert.swal({
                title: "确认删除选中的白名单吗?",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "提交",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true },
            function (isConfirm) {
                if (isConfirm) {
                    $http.post("transLimit/deleteTransLimitWhite","id="+id,
                        {headers: {'Content-Type': 'application/x-www-form-urlencoded'}})
                        .success(function(data){
                            if(data.status){
                                $scope.query();
                                $scope.notice(data.msg);
                            }else{
                                $scope.notice(data.msg);
                            }
                        })
                        .error(function(data){
                            $scope.notice(data.msg);
                        });

                }
            });
    };

    //页面绑定回车事件
    $document.bind("keypress", function(event) {
        $scope.$apply(function (){
            if(event.keyCode == 13){
                $scope.query();
            }
        })
    });
});